package dispatchers;

import java.io.*;
import java.sql.*;
import util.*;
import objects.*;
import java.util.*;

public class DBArticleDispatcher{
	private DBManager dbManager;
	private String tableName;

	public DBArticleDispatcher(DBManager dbManager, String tableName){
		this.dbManager = dbManager;
		this.tableName = tableName;
	}
	
	private Statement getStatement(){
		try{
			return dbManager.getConnection().createStatement();
		}catch(SQLException e){
			System.out.println(e);
			return null;
		}
	}
	
	
	public LinkedList<Article> getArticles(){	
		String query = "SELECT articleID, title, body FROM " + tableName + ";";
		
		ResultSet resultSet;
		LinkedList<Article> articleList = new LinkedList<Article>();
		try{
			resultSet = getStatement().executeQuery(query);	
			
			while(resultSet.next()){
				Article article = new Article();
				articleList.add(article);
				article.setArticleID(resultSet.getInt("articleID"));
				article.setTitle(resultSet.getString("title"));
				article.setBody(resultSet.getString("body"));
			}
		}catch(SQLException e){
			System.out.println(e);
			return null;
		}
		return articleList;
	}
	
	
	
	
	public Article getArticle(int id){	
		String query = "SELECT title, body FROM "
		+tableName+" WHERE articleID = \'"+id+"\' ;";
		
		ResultSet resultSet;
		Article article=null;
		try{
			resultSet = getStatement().executeQuery(query);	
			if(!resultSet.next()){
				System.out.println("post not found");
				return null;
			}
					
			article = new Article();			
			article.setArticleID(id);
			article.setTitle(resultSet.getString("title"));
			article.setBody(resultSet.getString("body"));
		}catch(SQLException e){
			System.out.println(e);
			return null;
		}
		return article;
	}
	
	
	public boolean setArticle(Article article){	
		String query = "UPDATE "+tableName+" SET "+
		"body=\'" + article.getBody() + "\', "+
		"title=\'" + article.getTitle() + "\'"+
		"WHERE articleID="+article.getArticleID()+";";
		
		try{
			getStatement().executeUpdate(query);
			return true;
		}catch(SQLException e){
			System.out.println(e);
			return false;
		}
	}
	
	
	public boolean addArticle(Article article){	
		String query = "INSERT INTO "+tableName+
			" (body, title) VALUES ("+
			"\'" + article.getBody() + "\', "+
			"\'" + article.getTitle() + "\'"+ ");";
		
		PrintWriter pw=null;
		try{
			pw = new PrintWriter(new OutputStreamWriter(System.out, "Cp866"), true);
		}catch(Exception e){
			System.out.println(e);
		}
		pw.println("статья передана базе данных с title:"+article.getTitle());
		
		try{
			getStatement().executeUpdate(query);
			return true;
		}catch(SQLException e){
			System.out.println(e);
			return false;
		}
	}
	
	public boolean deleteArticle(int articleID){
		String query = "DELETE FROM "+tableName+
			" WHERE articleID="+articleID+";";
		try{
			getStatement().executeUpdate(query);
			return true;
		}catch(SQLException e){
			System.out.println(e);
			return false;
		}
	}
	
	public static void main(String []arg){
		DBArticleDispatcher pd = new DBArticleDispatcher(new DBManager("anagram", "root", "root"), "articles");
		
		Article article = new Article(-1, "проверка", "кодировки в бд");
		pd.addArticle(article);
		
		article = pd.getArticle(14);
		
		PrintWriter pw=null;
		try{
			pw = new PrintWriter(new OutputStreamWriter(System.out, "Cp866"), true);
		}catch(Exception e){
			System.out.println(e);
		}
		pw.println(article.getTitle());
	}
}